/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.qb9.flashlib.easing.effects
{
	import com.qb9.flashlib.math.Random;
	import com.qb9.flashlib.tasks.ITask;
	import com.qb9.flashlib.tasks.TimeBasedTask;

	public class Vibrate extends TimeBasedTask
	{
		private var object:Object;
		
		private var dx:Number;
		private var dy:Number;
		
		private var lastDx:Number = 0;
		private var lastDy:Number = 0;
		
		public function Vibrate(object:Object, duration:uint, dx:Number=3, dy:Number=0)
		{
			super(duration);
			
			this.object = object;
			this.dx = dx;
			this.dy = dy;
		}
		
		override protected function updateState():void
		{
			restore();
			
			lastDx = Random.randrange(-dx, dx);
			lastDy = Random.randrange(-dy, dy);
			
			object.x += lastDx;
			object.y += lastDy;
		}
		
		private function restore():void
		{
			object.x -= lastDx;
			object.y -= lastDy;
		}
		
		override public function clone():ITask
		{
			return new Vibrate(object, duration, dx, dy);
		}
		
		override public function dispose():void
		{
			restore();
			object = null;			
			super.dispose();
		}
		
	}
}